/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.crt.handler;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.seari.bean.crt.CRTDataBean;
import com.seari.crt.CRTClient;
import com.seari.pa.handler.PaClientHandler;
import com.seari.utils.JedisPoolUtils;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import redis.clients.jedis.Jedis;

/**
 *
 * @author yyw
 */
public class CRTHandler extends ChannelInboundHandlerAdapter {

	protected static Logger logger = LoggerFactory.getLogger(PaClientHandler.class);
	CRTClient client;
	Jedis jedis = JedisPoolUtils.getJedis();

	public CRTHandler() {
		super();
	}

	public CRTHandler(CRTClient client) {
		this.client = client;
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			CRTDataBean result = (CRTDataBean) msg;
			logger.info("CRT channelRead:" + result.toString());
			jedis.set("crt", result.toString());
		} finally {
			// 抛弃收到的数据
			ReferenceCountUtil.release(msg);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		closeJedis();
	}

	public void closeJedis() {
		if (null != jedis) {
			jedis.disconnect();
			jedis.close();
		}
	}
	

}
